<template>
  <div>
    <el-button
      type="danger"
      @click="showEdit('add')"
      v-if="userInfo.roleType == 1"
      >新增分类</el-button
    >
    <Table
      :columns="columns"
      :showPagination="false"
      :dataSource="tableData"
      :fetch="loadDataList"
      :options="tableOptions"
    >
      <template #cover="{ index, row }">
        <Cover :cover="row.cover"></Cover>
      </template>
      <template #op="{ index, row }" >
        <div class="op" v-if="userInfo.roleType==1">
          <a
            href="javascript:void(0)"
            class="a-link"
            @click="showEdit('update', row)"
            >修改</a
          >
          <el-divider direction="vertical"></el-divider>
          <a href="javascript:void(0)" class="a-link" @click="del(row)">删除</a>
          <el-divider direction="vertical"></el-divider>
          <a
            href="javascript:void(0)"
            :class="[index == 0 ? 'not-allow' : 'a-link']"
            @click="changeSort(index, 'up')"
            >上移</a
          >
          <el-divider direction="vertical"></el-divider>
          <a
            href="javascript:void(0)"
            :class="[
              index == tableData.list.length - 1 ? 'not-allow' : 'a-link',
            ]"
            @click="changeSort(index, 'down')"
            >下移</a
          >
        </div>
      </template>
    </Table>
    <Dialog
      :show="dialogConfig.show"
      :title="dialogConfig.title"
      :buttons="dialogConfig.buttons"
      @close="dialogConfig.show = false"
      width="500px"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formDataRef"
        label-width="80px"
      >
        <el-form-item label="名称" prop="categoryName">
          <el-input placeholder="请输入名称" v-model="formData.categoryName">
          </el-input>
        </el-form-item>
        <el-form-item label="封面" prop="cover">
          <CoverUpload v-model="formData.cover"></CoverUpload>
        </el-form-item>
        <el-form-item label="简介" prop="categoryDesc">
          <el-input
            v-model="formData.categoryDesc"
            type="textarea"
            :autoSize="{ minRows: 4, maxRows: 4 }"
            placeholder="请输入简介"
          >
          </el-input>
        </el-form-item>
      </el-form>
    </Dialog>
  </div>
</template>

<script setup>
import { getCurrentInstance, nextTick, reactive, ref } from "vue";
import CoverUpload from "../../components/CoverUpload.vue";
const { proxy } = getCurrentInstance();
const columns = [
  {
    prop: "cover",
    label: "封面",
    width: 100,
    scopedSlots: "cover",
    align: "center",
  },
  { prop: "categoryName", label: "名称", width: 150, align: "center" },
  { prop: "categoryDesc", label: "简介", align: "center" },
  { prop: "blogCount", label: "博客数量", width: 100, align: "center" },
  { prop: "op", label: "操作", width: 200, align: "center", scopedSlots: "op" },
];

const api = {
  // 获取博客分类列表
  loadDataList: "/category/loadAllCategory4Blog",
  // 保存分类
  saveCategory: "/category/saveCategory4Blog",
  // 删除分类
  delCategory: "/category/delCategory4Blog",
  // 修改分类的排序
  changSort: "/category/changeCategorySort4Blog",
};
const tableData = reactive({});
const userInfo = ref(proxy.VueCookies.get("userInfo") || {});
const tableOptions = {
  extHeight: 10,
};
const loadDataList = async () => {
  let result = await proxy.Request({
    url: api.loadDataList,
  });
  if (!result) {
    return;
  }
  tableData.list = result.data;
  // console.log(result);
};
//新增修改
const dialogConfig = reactive({
  show: false,
  title: "我是标题",
  buttons: [
    {
      type: "danger",
      text: "确定",
      click: (e) => {
        submitForm();
        // console.log(userInfo);
      },
    },
  ],
});
const formDataRef = ref(null);
const formData = reactive({});
const rules = {
  categoryName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
};
const showEdit = (type, data) => {
  dialogConfig.show = true;
  nextTick(() => {
    formDataRef.value.resetFields();
    if (type == "add") {
      dialogConfig.title = "新增分类";
      formData.categoryId = "";
      // Object
    } else if (type == "update") {
      dialogConfig.title = "编辑分类";
      Object.assign(formData, data);
    }
  });
};
const submitForm = () => {
  formDataRef.value.validate(async (valid) => {
    if (!valid) {
      return;
    }
    let params = {};
    Object.assign(params, formData);
    let result = await proxy.Request({
      url: api.saveCategory,
      params,
    });
    if (!result) {
      return;
    }
    dialogConfig.show = false;
    proxy.Message.success("保存成功");
    loadDataList();
  });
};
//删除
const del = (data) => {
  proxy.Confirm(`你确定要删除：${data.categoryName} 吗？`, async () => {
    let result = await proxy.Request({
      url: api.delCategory,
      params: { categoryId: data.categoryId },
    });
    if (!result) {
      return;
    }
    proxy.Message.success("删除成功");
    loadDataList();
  });
  loadDataList();
};
//排序
const changeSort = async (index, type) => {
  let categoryList = tableData.list;
  if (
    (type === "down" && index === categoryList.length - 1) ||
    (type === "up" && index === 0)
  ) {
    return;
  }
  let temp = categoryList[index];
  let number = type == "down" ? 1 : -1;

  categoryList.splice(index, 1);
  categoryList.splice(index + number, 0, temp);
  // console.log("JSON: " + categoryList);
  let result = await proxy.Request({
    url: api.changSort,
    dataType: "json",
    params: categoryList,
  });
  if (!result) {
    return;
  }
  proxy.Message.success("重新排序成功");
  loadDataList();
};
</script>

<style lang="scss">
// .not-allow {
//   cursor: not-allowed;
//   color: #ddd;
//   text-decoration: none;
// }
</style>
